import router from "@/router";
import Vue from "vue";
import Vuex from "vuex";
import * as api from "@/api/users"

import * as permissionApi from '../api/permissions'
import recursionRoutes from "@/utils/recursionRoutes";//生成用户路由的递归函数
import allRoutes from "@/router/generateRoutes"; //所有路由
import dynamicRoute from "@/router/dynamicRoutes"
import createPersistedState from "vuex-persistedstate"


Vue.use(Vuex);
let userInfo = localStorage.getItem('w-userInfo') || "{}"
export default new Vuex.Store({
  state: {
    userInfo: JSON.parse(userInfo),
    userMenu: [],//侧边栏菜单
    breadCrumb: [], //面包屑数据
    stuData: [],
    dialogVisible: false,//控制dialog显示隐藏的值
    dialogTitle: "增加学员信息",//控制dialog的标题
    loading: true,
    currentRow: {},//编辑的时候，dialog回显的表格数据
    stuTotal: 0,//总条目
    stuPageSize: 10,//每页显示的数量
    currentProduct: {},//当前充值的产品对象
  },
  getters: {},
  mutations: {
    //设置当前充值的产品
    SET_CURRENT_PRODUCT(state,payload){
      state.currentProduct=payload
    },

    //表格显示loading动画
    SHOW_LOADING(state) {
      state.loading = true;

    },
    //表格隐藏loading动画
    HIDE_LOADING(state) {
      state.loading = false;
    },

    //设置学员项目管理表格中的数据
    SET_STU_DATA(state, payload) {
      state.stuData = payload.data;//设置学员列表
      state.loading = false;
      state.stuTotal = payload.total;//设置学员分页总数
    },
    SHOW_DIALOG(state) {
      state.dialogVisible = true;
    },
    HIDE_DIALOG(state) {
      state.dialogVisible = false;
    },
    //切换标题信息
    CHANGE_DIALOG_TITLE(state, payload) {
      state.dialogTitle = payload
    },
    // 保存用户信息
    SET_USER_INFO(state, payload) {
      state.userInfo = payload;
    },
    //设置菜单 
    SET_MENULIST(state, payload) {
      state.userMenu = [...payload];
      // console.log(state.sideMenu);
      let target = dynamicRoute.find(item => item.path === "/")
      // console.logs(state.sideMenu);
      target.children = [...target.children, ...payload]
      router.addRoute(target)


      // console.log(router.getRoutes());
    },
    //设置面包屑
    SET_BREADCRUBM(state, payload) {
      state.breadCrumb = payload;
      // console.log(payload);
    },

    //修改dialog回显数据
    SET_CURRENT_ROW(state, payload) {
      state.currentRow = { ...payload }

    }

  },
  actions: {
    async FETCH_MENULIST(context, payload) {
      return permissionApi.getMenuList().then((res) => {
        // console.log(res.data.menuList);
        let result = recursionRoutes(allRoutes[0].children, res.data.menuList);
        // console.log(result);
        context.commit('SET_MENULIST', result)
      });
    },
    GET_STULIST({ commit }, payload = {}) {//获取学员列表
      commit('SHOW_LOADING')
      let { page = 1, count = 5, classes = "" } = payload
      api.getStulistApi(page, count, classes)
        .then(res => {
          // console.log(res);
          commit('SET_STU_DATA', res.data)
        })

    },
  },
  modules: {

  },
  plugins:[
    createPersistedState({
      storage:window.localStorage,
      key:"why",
      paths:['userInfo','currentProduct']
    })
  ]

});



